package ServerStuff;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.sql.Date;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONException;
import org.json.JSONObject;

import DataStorageClasses.Client;
import DataStorageClasses.Job;
import Database.DatabaseConnector;


@WebServlet("/ClientChangesServlet")
public class ClientChangesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	{
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = req.getSession();
		String busName = (String) session.getAttribute("username");
		DatabaseConnector db = new DatabaseConnector();
		String temp = (String) req.getParameter("clientID");
		long clientID = Long.parseLong(temp);

		String first = req.getParameter("first");
		String last = req.getParameter("last");
		String email = req.getParameter("email");
		String tel1 = req.getParameter("tel1");
		String tel2 = req.getParameter("tel2");
		String cell = req.getParameter("cell");
		String address = req.getParameter("address");
		String prov = req.getParameter("prov");
		String city = req.getParameter("city");
		String post = req.getParameter("post");
		
		try {
			db.updateClient(clientID, first, last, email, address, city, prov, post, tel1, tel2, cell);
			//db.updateJob(jobID, estPrice, actPrice, amtPaid, expStartDate, actStartDate, expFinishDate, actFinishDate, street, city, province);
			Client client = db.getClient(clientID, busName);
			JSONObject result = new JSONObject();
			result.put("reset", true);
			result.put("first", client.getFirstName());
			result.put("last", client.getLastName());
			result.put("tel1", client.getTel1() );
			result.put("tel2", client.getTel2() );
			result.put("email", client.getEmail() );
			result.put("city", client.getCity() );
			result.put("postal", client.getPostalCode() );
			result.put("province", client.getProvince() );
			result.put("street", client.getStreetAd() );
			result.put("cell", client.getCell() );
			result.put("clientID", clientID);
			
			try {
				res.getWriter().print(result);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			res.setStatus(HttpURLConnection.HTTP_ACCEPTED);
		}catch(JSONException e) { 
			e.printStackTrace();
		} 
		
		response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
